//
// Created on 2024/8/6.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be
// found, please include "napi/native_api.h".

#ifndef ARKUI_C_API_TLANG_LIST_H
#define ARKUI_C_API_TLANG_LIST_H

#include "ArkUIListNode.h"
#include "../tlang/tvm/rt_code/NativeMethodTable.h"
#include "TLangText.h"
#include "src/TLangListItem.h"
#include <memory>

using std::shared_ptr;

class ListWrapper {
    shared_ptr<NativeModule::ArkUIListNode> target;

public:
    ListWrapper() { target = std::make_shared<NativeModule::ArkUIListNode>(); }

    static void bind(shared_ptr<RtCode::NativeMethodTable> nativeMethodTable) {
        nativeMethodTable->add("List", "create", create);
        nativeMethodTable->add("List", "addChild", addChild);
        nativeMethodTable->add("List", "width", setWidth);
        nativeMethodTable->add("List", "height", setHeight);
        nativeMethodTable->add("List", "render", render);
    }

    shared_ptr<NativeModule::ArkUIListNode> getTarget() { return target; }

    static void create(RtData::Frame &frame) {
        auto *wrapper = new ListWrapper();
        frame.pushRef(reinterpret_cast<RtData::Object *>(wrapper));
    }

    static void setWidth(RtData::Frame &frame) {
        auto *self = frame.getSlot(0).getRef();
        auto *wrapper = reinterpret_cast<ListWrapper *>(self);
        auto arg = frame.popValue().value.int64;
        wrapper->target->SetWidth(arg);
    }

    static void setHeight(RtData::Frame &frame) {
        auto *self = frame.getSlot(0).getRef();
        auto *wrapper = reinterpret_cast<ListWrapper *>(self);
        auto arg = frame.popValue().value.int64;
        wrapper->target->SetHeight(arg);
    }

    static void addChild(RtData::Frame &frame) {
        auto *self = frame.getSlot(0).getRef();
        auto *wrapper = reinterpret_cast<ListWrapper *>(self);
        auto arg = reinterpret_cast<ListItemWrapper *>(frame.popRef());
        wrapper->target->AddChild(arg->getTarget());
    }

    static void render(RtData::Frame &frame) {
        auto *self = frame.getSlot(0).getRef();
        auto *wrapper = reinterpret_cast<ListWrapper *>(self);
        shared_ptr<NativeModule::ArkUIBaseNode> rootNode = wrapper->target;
        NativeModule::NativeEntry::GetInstance()->SetRootNode(rootNode);
    }
};

#endif // ARKUI_C_API_TLANG_LIST_H
